package com.hand.demo.contoller;

import com.hand.demo.entity.Novel;
import com.hand.demo.repository.NovelRepository;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.Objects;
import java.util.Optional;

@RestController
@RequestMapping(value = "book")
@Slf4j
public class BookController {
    @Autowired
    private NovelRepository repository;

    @GetMapping(value = "/{id}/get")
    public ResponseEntity get(@PathVariable(name = "id",required = true)String id){
        if(id.isEmpty()){
            return new ResponseEntity(HttpStatus.NOT_FOUND);
        }

        Optional<Novel> search = repository.findById(id);
        if (!search.isPresent()){
            return new ResponseEntity(HttpStatus.NOT_FOUND);
        }
        log.info("--getID={},res={}",id,search.get());
        return ResponseEntity.ok(search.get());
    }

    @PostMapping("/add")
    public ResponseEntity add(@RequestBody Novel novel){
        if (Objects.isNull(novel)){
            return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
        }
        Novel save = this.repository.save(novel);
        return ResponseEntity.ok(save);
    }


    @DeleteMapping(value = "/{id}/delete")
    public ResponseEntity delete(@PathVariable(name = "id",required = true)String id){
        if(id.isEmpty()){
            return new ResponseEntity(HttpStatus.NOT_FOUND);
        }
        repository.deleteById(id);

        return ResponseEntity.ok(true);
    }

    @PutMapping("/update")
    public ResponseEntity update(@RequestBody Novel novel){
        if (Objects.isNull(novel)){
            return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
        }
        Novel save = this.repository.save(novel);
        return ResponseEntity.ok(save);
    }

    @PostMapping("query")
    public ResponseEntity query(
            @RequestParam(name = "author",required = false)String author,
            @RequestParam(name = "title",required = false)String title,
            @RequestParam(name = "gt_word_count",defaultValue = "0" )int wordCountMin,
            @RequestParam(name = "lt_word_count",required = false )Integer wordCountMax
    ){
        BoolQueryBuilder builder = QueryBuilders.boolQuery();
        if (author!=null){
            builder.must(QueryBuilders.matchQuery("author",author));
        }
        if (title!=null){
            builder.must(QueryBuilders.matchQuery("title",title));
        }
        RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("word_count");
        rangeQueryBuilder.from(wordCountMin);
        if (wordCountMax!=null &&  wordCountMax>0){
            rangeQueryBuilder.to(wordCountMax);
        }
        builder.filter(rangeQueryBuilder);


        Page<Novel> search = this.repository.search(builder, Query.DEFAULT_PAGE);
        return ResponseEntity.ok(search);
    }

}
